import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:xm_app/common/logger/LoggerUtil.dart';
import 'package:xm_app/common/ui/AppTheme.dart';
import 'package:xm_app/common/ui/ui_adapter.dart';
import 'package:xm_app/common/ui/ui_image.dart';
import 'package:xm_app/model/HomeData.dart';
import 'package:xm_app/routes/AppRoutes.dart';

import 'CommonText.dart';

class CommonGrid extends StatelessWidget {
  final List<HomeDataItem> list;
  final bool isShowTag;
  final Function(HomeDataItem) onTapItem;
  const CommonGrid({Key key, this.list, this.isShowTag = false, this.onTapItem}) : super(key: key);

  int itemCount() {
    return list?.length ?? 0;
  }

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      padding: EdgeInsetsDirectional.zero,
      shrinkWrap: true,
      physics: NeverScrollableScrollPhysics(),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3,
        mainAxisSpacing: 5,
        crossAxisSpacing: 5,
        childAspectRatio: 200 / 360,
      ),
      itemBuilder: (context, index) {
        return GestureDetector(
          onTap: () {
            if (onTapItem != null) {
              onTapItem(list[index]);
            }
            toVodDetailPage(id: list[index].vodID);
          },
          child: CommonGridItem(item: list[index], isShowTag: isShowTag),
        );
      },
      itemCount: itemCount(),
    );
  }
}

class CommonGridItem extends StatelessWidget {
  final HomeDataItem item;
  final bool isShowTag;
  const CommonGridItem({Key key, this.item, this.isShowTag}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Card(
      margin: EdgeInsetsDirectional.zero,
      child: Container(
        color: Colors.white,
        child: Column(
          children: [
            AspectRatio(
              aspectRatio: 260 / 360,
              child: Stack(
                children: [
                  buildItemImage(item.vodPic),
                  if (isShowTag)
                    Positioned(
                      top: 5,
                      left: 5,
                      child: Row(
                        children: [
                          buildTag(text: item.vodYear, color: AppTheme.pinkColor, radius: 2),
                          SizedBox(width: getWidth(2)),
                          buildTag(text: item.vodClass.typeName, radius: 2),
                        ],
                      ),
                    ),
                  if (isShowTag)
                    Positioned(
                      bottom: 0,
                      right: 0,
                      child: Container(
                        width: getWidth(200),
                        height: getHeight(15),
                        padding: EdgeInsets.only(right: 5),
                        alignment: Alignment.topRight,
                        color: Colors.black.withOpacity(.15),
                        child: CommonText(
                          item.vodRemarks,
                          txtColor: Colors.white,
                          txtSize: getSp(11),
                        ),
                      ),
                    )
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.all(5),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    item.vodName,
                    style: TextStyle(fontSize: getSp(12)),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                  Text(
                    item.vodActor,
                    style: TextStyle(fontSize: getSp(11)),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}

buildTag({String text, Color color = AppTheme.blue, double radius = 8}) {
  if (text == null) {
    return Container();
  }
  return Container(
    height: getHeight(15),
    alignment: Alignment.center,
    padding: EdgeInsets.symmetric(
      horizontal: 5,
    ),
    constraints: BoxConstraints(
      minWidth: getWidth(30),
    ),
    child: Text(
      text,
      textAlign: TextAlign.center,
      style: TextStyle(fontSize: getSp(10), color: Colors.white),
    ),
    decoration: BoxDecoration(borderRadius: BorderRadius.circular(radius), color: color),
  );
}

buildUpdateText(String text) {
  return Positioned(
    bottom: 0,
    right: 0,
    left: 0,
    child: Container(
      width: double.infinity,
      height: 20,
      padding: EdgeInsets.only(right: 5),
      alignment: Alignment.topRight,
      color: Colors.black.withOpacity(.15),
      child: CommonText(
        text,
        txtColor: Colors.white,
        txtSize: getSp(11),
      ),
    ),
  );
}
